import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import 'package:zrjz/comm/http/http_result.dart';
import 'package:zrjz/comm/utils/log_utils.dart';
import 'package:zrjz/comm/utils/util.dart';
import 'package:zrjz/comm/widget/dialog/app_default_dialog.dart';
import 'package:zrjz/globalService/user_service.dart';
import 'package:zrjz/router/page_router.dart';

import 'setting_state.dart';

class SettingController extends GetxController {

  final SettingState state = SettingState();

  /// 退出登录的弹窗
  Future<void> showLogoutDialog() async {
    SmartDialog.show(
      usePenetrate: false,
      keepSingle :true,
      builder: (context) =>
          AppDefaultDialog(
            "你确定你要退出登录吗？".tr,
            confirmAction: () {
              // 调用接口退出登录
              _userLogout();
            },
          ),
    );
  }

  /// 调用接口退出登录
  void _userLogout() async {
    // SmartDialog.showLoading();
    // HttpResult result = await mainRepository.userLogout();
    // SmartDialog.dismiss(status: SmartStatus.loading);
    //
    // //处理数据
    // if (result.isSuccess) {
    //   Log.d("退出登录 => 成功");
    //   UserService.to.handleLogoutParams();
    //   Get.offAllNamed(RouterPath.AUTH_LOGIN); //关闭全部页面进入登录页面，不能改
    // } else {
    //   Log.d("退出登录 => 失败：${result.errorMsg}");
    // }
  }

  @override
  void onReady() {
    super.onReady();
    calculateCacheSize();
  }

  //计算当前 App 的 Cache 缓存
  void calculateCacheSize() async {
    final size = await Utils.getAppCacheSize();
    state.appSize = Utils.formatBytes(size);
    update();
  }

  //清除缓存
  void clearCache() async {
    SmartDialog.show(
      usePenetrate: false,
      builder: (context) => AppDefaultDialog(
        "你确定你要清除应用缓存吗？".tr,
        confirmAction: () async {
          await Utils.clearAppCache();
          state.appSize = '0 B';
          update();
        },
      ),
    );
  }
}
